
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/tutorial/matrices.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:57 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Matrices &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="matrices.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Advanced Expression Manipulation" href="manipulation.html" />
    <link rel="prev" title="Solvers" href="solvers.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="manipulation.html" title="Advanced Expression Manipulation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="solvers.html" title="Solvers"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Matrices</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="matrices">
<span id="id1"></span><h1>Matrices<a class="headerlink" href="#matrices" title="Permalink to this headline">¶</a></h1>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init_printing</span><span class="p">(</span><span class="n">use_unicode</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>To make a matrix in SymPy, use the <code class="docutils literal notranslate"><span class="pre">Matrix</span></code> object.  A matrix is constructed
by providing a list of row vectors that make up the matrix.  For example,
to construct the matrix</p>
<div class="math notranslate nohighlight">
\[\begin{split}\left[\begin{array}{cc}1 &amp; -1\\3 &amp; 4\\0 &amp; 2\end{array}\right]\end{split}\]</div>
<p>use</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">]])</span>
<span class="go">⎡1  -1⎤</span>
<span class="go">⎢     ⎥</span>
<span class="go">⎢3  4 ⎥</span>
<span class="go">⎢     ⎥</span>
<span class="go">⎣0  2 ⎦</span>
</pre></div>
</div>
<p>To make it easy to make column vectors, a list of elements is considered to be
a column vector.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Matrix</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="go">⎡1⎤</span>
<span class="go">⎢ ⎥</span>
<span class="go">⎢2⎥</span>
<span class="go">⎢ ⎥</span>
<span class="go">⎣3⎦</span>
</pre></div>
</div>
<p>Matrices are manipulated just like any other object in SymPy or Python.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">*</span><span class="n">N</span>
<span class="go">⎡5⎤</span>
<span class="go">⎢ ⎥</span>
<span class="go">⎣3⎦</span>
</pre></div>
</div>
<p>One important thing to note about SymPy matrices is that, unlike every other
object in SymPy, they are mutable.  This means that they can be modified in
place, as we will see below.  The downside to this is that <code class="docutils literal notranslate"><span class="pre">Matrix</span></code> cannot
be used in places that require immutability, such as inside other SymPy
expressions or as keys to dictionaries.  If you need an immutable version of
<code class="docutils literal notranslate"><span class="pre">Matrix</span></code>, use <code class="docutils literal notranslate"><span class="pre">ImmutableMatrix</span></code>.</p>
<section id="basic-operations">
<h2>Basic Operations<a class="headerlink" href="#basic-operations" title="Permalink to this headline">¶</a></h2>
<p>Here are some basic operations on <code class="docutils literal notranslate"><span class="pre">Matrix</span></code>.</p>
<section id="shape">
<h3>Shape<a class="headerlink" href="#shape" title="Permalink to this headline">¶</a></h3>
<p>To get the shape of a matrix, use <a class="reference internal" href="../modules/codegen.html#sympy.codegen.fnodes.shape" title="sympy.codegen.fnodes.shape"><code class="xref py py-func docutils literal notranslate"><span class="pre">shape()</span></code></a> function.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">shape</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1   2  3⎤</span>
<span class="go">⎢        ⎥</span>
<span class="go">⎣-2  0  4⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">shape</span><span class="p">(</span><span class="n">M</span><span class="p">)</span>
<span class="go">(2, 3)</span>
</pre></div>
</div>
</section>
<section id="accessing-rows-and-columns">
<h3>Accessing Rows and Columns<a class="headerlink" href="#accessing-rows-and-columns" title="Permalink to this headline">¶</a></h3>
<p>To get an individual row or column of a matrix, use <code class="docutils literal notranslate"><span class="pre">row</span></code> or <code class="docutils literal notranslate"><span class="pre">col</span></code>.  For
example, <code class="docutils literal notranslate"><span class="pre">M.row(0)</span></code> will get the first row. <code class="docutils literal notranslate"><span class="pre">M.col(-1)</span></code> will get the last
column.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">row</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">[1  2  3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">col</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">⎡3⎤</span>
<span class="go">⎢ ⎥</span>
<span class="go">⎣4⎦</span>
</pre></div>
</div>
</section>
<section id="deleting-and-inserting-rows-and-columns">
<h3>Deleting and Inserting Rows and Columns<a class="headerlink" href="#deleting-and-inserting-rows-and-columns" title="Permalink to this headline">¶</a></h3>
<p>To delete a row or column, use <code class="docutils literal notranslate"><span class="pre">row_del</span></code> or <code class="docutils literal notranslate"><span class="pre">col_del</span></code>.  These operations
will modify the Matrix <strong>in place</strong>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">col_del</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡2  3⎤</span>
<span class="go">⎢    ⎥</span>
<span class="go">⎣0  4⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">row_del</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">[2  3]</span>
</pre></div>
</div>
<p>To insert rows or columns, use <code class="docutils literal notranslate"><span class="pre">row_insert</span></code> or <code class="docutils literal notranslate"><span class="pre">col_insert</span></code>.  These
operations <strong>do not</strong> operate in place.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">[2  3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">row_insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">]]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡2  3⎤</span>
<span class="go">⎢    ⎥</span>
<span class="go">⎣0  4⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">col_insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">Matrix</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1   2  3⎤</span>
<span class="go">⎢        ⎥</span>
<span class="go">⎣-2  0  4⎦</span>
</pre></div>
</div>
<p>Unless explicitly stated, the methods mentioned below do not operate in
place. In general, a method that does not operate in place will return a new
<code class="docutils literal notranslate"><span class="pre">Matrix</span></code> and a method that does operate in place will return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</section>
</section>
<section id="basic-methods">
<h2>Basic Methods<a class="headerlink" href="#basic-methods" title="Permalink to this headline">¶</a></h2>
<p>As noted above, simple operations like addition and multiplication are done
just by using <code class="docutils literal notranslate"><span class="pre">+</span></code>, <code class="docutils literal notranslate"><span class="pre">*</span></code>, and <code class="docutils literal notranslate"><span class="pre">**</span></code>.  To find the inverse of a matrix, just
raise it to the <code class="docutils literal notranslate"><span class="pre">-1</span></code> power.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">7</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">+</span> <span class="n">N</span>
<span class="go">⎡1   6 ⎤</span>
<span class="go">⎢      ⎥</span>
<span class="go">⎣-2  10⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">*</span><span class="n">N</span>
<span class="go">⎡0  24⎤</span>
<span class="go">⎢     ⎥</span>
<span class="go">⎣0  15⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">3</span><span class="o">*</span><span class="n">M</span>
<span class="go">⎡3   9⎤</span>
<span class="go">⎢     ⎥</span>
<span class="go">⎣-6  9⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">**</span><span class="mi">2</span>
<span class="go">⎡-5  12⎤</span>
<span class="go">⎢      ⎥</span>
<span class="go">⎣-8  3 ⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">**-</span><span class="mi">1</span>
<span class="go">⎡1/3  -1/3⎤</span>
<span class="go">⎢         ⎥</span>
<span class="go">⎣2/9  1/9 ⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="o">**-</span><span class="mi">1</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">NonInvertibleMatrixError</span>: <span class="n">Matrix det == 0; not invertible.</span>
</pre></div>
</div>
<p>To take the transpose of a Matrix, use <code class="docutils literal notranslate"><span class="pre">T</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1  2  3⎤</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎣4  5  6⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">T</span>
<span class="go">⎡1  4⎤</span>
<span class="go">⎢    ⎥</span>
<span class="go">⎢2  5⎥</span>
<span class="go">⎢    ⎥</span>
<span class="go">⎣3  6⎦</span>
</pre></div>
</div>
</section>
<section id="matrix-constructors">
<h2>Matrix Constructors<a class="headerlink" href="#matrix-constructors" title="Permalink to this headline">¶</a></h2>
<p>Several constructors exist for creating common matrices.  To create an
identity matrix, use <code class="docutils literal notranslate"><span class="pre">eye</span></code>.  <code class="docutils literal notranslate"><span class="pre">eye(n)</span></code> will create an <span class="math notranslate nohighlight">\(n\times n\)</span> identity matrix.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">⎡1  0  0⎤</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎢0  1  0⎥</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎣0  0  1⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eye</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="go">⎡1  0  0  0⎤</span>
<span class="go">⎢          ⎥</span>
<span class="go">⎢0  1  0  0⎥</span>
<span class="go">⎢          ⎥</span>
<span class="go">⎢0  0  1  0⎥</span>
<span class="go">⎢          ⎥</span>
<span class="go">⎣0  0  0  1⎦</span>
</pre></div>
</div>
<p>To create a matrix of all zeros, use <code class="docutils literal notranslate"><span class="pre">zeros</span></code>.  <code class="docutils literal notranslate"><span class="pre">zeros(n,</span> <span class="pre">m)</span></code> creates an
<span class="math notranslate nohighlight">\(n\times m\)</span> matrix of <span class="math notranslate nohighlight">\(0\)</span>s.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">zeros</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">⎡0  0  0⎤</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎣0  0  0⎦</span>
</pre></div>
</div>
<p>Similarly, <code class="docutils literal notranslate"><span class="pre">ones</span></code> creates a matrix of ones.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ones</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">⎡1  1⎤</span>
<span class="go">⎢    ⎥</span>
<span class="go">⎢1  1⎥</span>
<span class="go">⎢    ⎥</span>
<span class="go">⎣1  1⎦</span>
</pre></div>
</div>
<p>To create diagonal matrices, use <code class="docutils literal notranslate"><span class="pre">diag</span></code>.  The arguments to <code class="docutils literal notranslate"><span class="pre">diag</span></code> can be
either numbers or matrices.  A number is interpreted as a <span class="math notranslate nohighlight">\(1\times 1\)</span>
matrix. The matrices are stacked diagonally.  The remaining elements are
filled with <span class="math notranslate nohighlight">\(0\)</span>s.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diag</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">⎡1  0  0⎤</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎢0  2  0⎥</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎣0  0  3⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diag</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">Matrix</span><span class="p">([</span><span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">5</span><span class="p">]))</span>
<span class="go">⎡-1  0  0  0⎤</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢0   1  1  0⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢0   1  1  0⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢0   0  0  5⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢0   0  0  7⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎣0   0  0  5⎦</span>
</pre></div>
</div>
</section>
<section id="advanced-methods">
<h2>Advanced Methods<a class="headerlink" href="#advanced-methods" title="Permalink to this headline">¶</a></h2>
<section id="determinant">
<h3>Determinant<a class="headerlink" href="#determinant" title="Permalink to this headline">¶</a></h3>
<p>To compute the determinant of a matrix, use <code class="docutils literal notranslate"><span class="pre">det</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1  0   1⎤</span>
<span class="go">⎢        ⎥</span>
<span class="go">⎢2  -1  3⎥</span>
<span class="go">⎢        ⎥</span>
<span class="go">⎣4  3   2⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">det</span><span class="p">()</span>
<span class="go">-1</span>
</pre></div>
</div>
</section>
<section id="rref">
<h3>RREF<a class="headerlink" href="#rref" title="Permalink to this headline">¶</a></h3>
<p>To put a matrix into reduced row echelon form, use <code class="docutils literal notranslate"><span class="pre">rref</span></code>.  <code class="docutils literal notranslate"><span class="pre">rref</span></code> returns
a tuple of two elements. The first is the reduced row echelon form, and the
second is a tuple of indices of the pivot columns.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">],</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">4</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1   0   1   3 ⎤</span>
<span class="go">⎢              ⎥</span>
<span class="go">⎢2   3   4   7 ⎥</span>
<span class="go">⎢              ⎥</span>
<span class="go">⎣-1  -3  -3  -4⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">rref</span><span class="p">()</span>
<span class="go">⎛⎡1  0   1    3 ⎤        ⎞</span>
<span class="go">⎜⎢              ⎥        ⎟</span>
<span class="go">⎜⎢0  1  2/3  1/3⎥, (0, 1)⎟</span>
<span class="go">⎜⎢              ⎥        ⎟</span>
<span class="go">⎝⎣0  0   0    0 ⎦        ⎠</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The first element of the tuple returned by <code class="docutils literal notranslate"><span class="pre">rref</span></code> is of type
<code class="docutils literal notranslate"><span class="pre">Matrix</span></code>. The second is of type <code class="docutils literal notranslate"><span class="pre">tuple</span></code>.</p>
</div>
</section>
<section id="nullspace">
<h3>Nullspace<a class="headerlink" href="#nullspace" title="Permalink to this headline">¶</a></h3>
<p>To find the nullspace of a matrix, use <code class="docutils literal notranslate"><span class="pre">nullspace</span></code>. <code class="docutils literal notranslate"><span class="pre">nullspace</span></code> returns a
<code class="docutils literal notranslate"><span class="pre">list</span></code> of column vectors that span the nullspace of the matrix.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1  2   3  0  0⎤</span>
<span class="go">⎢              ⎥</span>
<span class="go">⎣4  10  0  0  1⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">nullspace</span><span class="p">()</span>
<span class="go">⎡⎡-15⎤  ⎡0⎤  ⎡ 1  ⎤⎤</span>
<span class="go">⎢⎢   ⎥  ⎢ ⎥  ⎢    ⎥⎥</span>
<span class="go">⎢⎢ 6 ⎥  ⎢0⎥  ⎢-1/2⎥⎥</span>
<span class="go">⎢⎢   ⎥  ⎢ ⎥  ⎢    ⎥⎥</span>
<span class="go">⎢⎢ 1 ⎥, ⎢0⎥, ⎢ 0  ⎥⎥</span>
<span class="go">⎢⎢   ⎥  ⎢ ⎥  ⎢    ⎥⎥</span>
<span class="go">⎢⎢ 0 ⎥  ⎢1⎥  ⎢ 0  ⎥⎥</span>
<span class="go">⎢⎢   ⎥  ⎢ ⎥  ⎢    ⎥⎥</span>
<span class="go">⎣⎣ 0 ⎦  ⎣0⎦  ⎣ 1  ⎦⎦</span>
</pre></div>
</div>
</section>
<section id="columnspace">
<h3>Columnspace<a class="headerlink" href="#columnspace" title="Permalink to this headline">¶</a></h3>
<p>To find the columnspace of a matrix, use <code class="docutils literal notranslate"><span class="pre">columnspace</span></code>. <code class="docutils literal notranslate"><span class="pre">columnspace</span></code> returns a
<code class="docutils literal notranslate"><span class="pre">list</span></code> of column vectors that span the columnspace of the matrix.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span> <span class="p">,</span><span class="mi">1</span> <span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span> <span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡1  1  2⎤</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎢2  1  3⎥</span>
<span class="go">⎢       ⎥</span>
<span class="go">⎣3  1  4⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">columnspace</span><span class="p">()</span>
<span class="go">⎡⎡1⎤  ⎡1⎤⎤</span>
<span class="go">⎢⎢ ⎥  ⎢ ⎥⎥</span>
<span class="go">⎢⎢2⎥, ⎢1⎥⎥</span>
<span class="go">⎢⎢ ⎥  ⎢ ⎥⎥</span>
<span class="go">⎣⎣3⎦  ⎣1⎦⎦</span>
</pre></div>
</div>
</section>
<section id="eigenvalues-eigenvectors-and-diagonalization">
<h3>Eigenvalues, Eigenvectors, and Diagonalization<a class="headerlink" href="#eigenvalues-eigenvectors-and-diagonalization" title="Permalink to this headline">¶</a></h3>
<p>To find the eigenvalues of a matrix, use <code class="docutils literal notranslate"><span class="pre">eigenvals</span></code>.  <code class="docutils literal notranslate"><span class="pre">eigenvals</span></code>
returns a dictionary of <code class="docutils literal notranslate"><span class="pre">eigenvalue:</span> <span class="pre">algebraic_multiplicity</span></code> pairs (similar to the
output of <a class="reference internal" href="solvers.html#tutorial-roots"><span class="std std-ref">roots</span></a>).</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span>  <span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span>  <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span>  <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span>  <span class="mi">3</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span>
<span class="go">⎡3  -2  4   -2⎤</span>
<span class="go">⎢             ⎥</span>
<span class="go">⎢5  3   -3  -2⎥</span>
<span class="go">⎢             ⎥</span>
<span class="go">⎢5  -2  2   -2⎥</span>
<span class="go">⎢             ⎥</span>
<span class="go">⎣5  -2  -3  3 ⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">eigenvals</span><span class="p">()</span>
<span class="go">{-2: 1, 3: 1, 5: 2}</span>
</pre></div>
</div>
<p>This means that <code class="docutils literal notranslate"><span class="pre">M</span></code> has eigenvalues -2, 3, and 5, and that the
eigenvalues -2 and 3 have algebraic multiplicity 1 and that the eigenvalue 5
has algebraic multiplicity 2.</p>
<p>To find the eigenvectors of a matrix, use <code class="docutils literal notranslate"><span class="pre">eigenvects</span></code>.  <code class="docutils literal notranslate"><span class="pre">eigenvects</span></code>
returns a list of tuples of the form <code class="docutils literal notranslate"><span class="pre">(eigenvalue,</span> <span class="pre">algebraic_multiplicity,</span>
<span class="pre">[eigenvectors])</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">eigenvects</span><span class="p">()</span>
<span class="go">⎡⎛       ⎡⎡0⎤⎤⎞  ⎛      ⎡⎡1⎤⎤⎞  ⎛      ⎡⎡1⎤  ⎡0 ⎤⎤⎞⎤</span>
<span class="go">⎢⎜       ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥  ⎢  ⎥⎥⎟⎥</span>
<span class="go">⎢⎜       ⎢⎢1⎥⎥⎟  ⎜      ⎢⎢1⎥⎥⎟  ⎜      ⎢⎢1⎥  ⎢-1⎥⎥⎟⎥</span>
<span class="go">⎢⎜-2, 1, ⎢⎢ ⎥⎥⎟, ⎜3, 1, ⎢⎢ ⎥⎥⎟, ⎜5, 2, ⎢⎢ ⎥, ⎢  ⎥⎥⎟⎥</span>
<span class="go">⎢⎜       ⎢⎢1⎥⎥⎟  ⎜      ⎢⎢1⎥⎥⎟  ⎜      ⎢⎢1⎥  ⎢0 ⎥⎥⎟⎥</span>
<span class="go">⎢⎜       ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥⎥⎟  ⎜      ⎢⎢ ⎥  ⎢  ⎥⎥⎟⎥</span>
<span class="go">⎣⎝       ⎣⎣1⎦⎦⎠  ⎝      ⎣⎣1⎦⎦⎠  ⎝      ⎣⎣0⎦  ⎣1 ⎦⎦⎠⎦</span>
</pre></div>
</div>
<p>This shows us that, for example, the eigenvalue 5 also has geometric
multiplicity 2, because it has two eigenvectors.  Because the algebraic and
geometric multiplicities are the same for all the eigenvalues, <code class="docutils literal notranslate"><span class="pre">M</span></code> is
diagonalizable.</p>
<p>To diagonalize a matrix, use <code class="docutils literal notranslate"><span class="pre">diagonalize</span></code>. <code class="docutils literal notranslate"><span class="pre">diagonalize</span></code> returns a tuple
<span class="math notranslate nohighlight">\((P, D)\)</span>, where <span class="math notranslate nohighlight">\(D\)</span> is diagonal and <span class="math notranslate nohighlight">\(M = PDP^{-1}\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">P</span><span class="p">,</span> <span class="n">D</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">diagonalize</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">P</span>
<span class="go">⎡0  1  1  0 ⎤</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢1  1  1  -1⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢1  1  1  0 ⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎣1  1  0  1 ⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span>
<span class="go">⎡-2  0  0  0⎤</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢0   3  0  0⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎢0   0  5  0⎥</span>
<span class="go">⎢           ⎥</span>
<span class="go">⎣0   0  0  5⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">P</span><span class="o">*</span><span class="n">D</span><span class="o">*</span><span class="n">P</span><span class="o">**-</span><span class="mi">1</span>
<span class="go">⎡3  -2  4   -2⎤</span>
<span class="go">⎢             ⎥</span>
<span class="go">⎢5  3   -3  -2⎥</span>
<span class="go">⎢             ⎥</span>
<span class="go">⎢5  -2  2   -2⎥</span>
<span class="go">⎢             ⎥</span>
<span class="go">⎣5  -2  -3  3 ⎦</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">P</span><span class="o">*</span><span class="n">D</span><span class="o">*</span><span class="n">P</span><span class="o">**-</span><span class="mi">1</span> <span class="o">==</span> <span class="n">M</span>
<span class="go">True</span>
</pre></div>
</div>
<aside class="sidebar">
<p class="sidebar-title">Quick Tip</p>
<p><code class="docutils literal notranslate"><span class="pre">lambda</span></code> is a reserved keyword in Python, so to create a Symbol called
<span class="math notranslate nohighlight">\(\lambda\)</span>, while using the same names for SymPy Symbols and Python
variables, use <code class="docutils literal notranslate"><span class="pre">lamda</span></code> (without the <code class="docutils literal notranslate"><span class="pre">b</span></code>).  It will still pretty print
as <span class="math notranslate nohighlight">\(\lambda\)</span>.</p>
</aside>
<p>Note that since <code class="docutils literal notranslate"><span class="pre">eigenvects</span></code> also includes the eigenvalues, you should use
it instead of <code class="docutils literal notranslate"><span class="pre">eigenvals</span></code> if you also want the eigenvectors. However, as
computing the eigenvectors may often be costly, <code class="docutils literal notranslate"><span class="pre">eigenvals</span></code> should be
preferred if you only wish to find the eigenvalues.</p>
<p>If all you want is the characteristic polynomial, use <code class="docutils literal notranslate"><span class="pre">charpoly</span></code>.  This is
more efficient than <code class="docutils literal notranslate"><span class="pre">eigenvals</span></code>, because sometimes symbolic roots can be
expensive to calculate.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">lamda</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;lamda&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">M</span><span class="o">.</span><span class="n">charpoly</span><span class="p">(</span><span class="n">lamda</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">as_expr</span><span class="p">())</span>
<span class="go">       2</span>
<span class="go">(λ - 5) ⋅(λ - 3)⋅(λ + 2)</span>
</pre></div>
</div>
</section>
</section>
<section id="possible-issues">
<h2>Possible Issues<a class="headerlink" href="#possible-issues" title="Permalink to this headline">¶</a></h2>
<section id="zero-testing">
<h3>Zero Testing<a class="headerlink" href="#zero-testing" title="Permalink to this headline">¶</a></h3>
<p>If your matrix operations are failing or returning wrong answers,
the common reasons would likely be from zero testing.
If there is an expression not properly zero-tested,
it can possibly bring issues in finding pivots for gaussian elimination,
or deciding whether the matrix is inversible,
or any high level functions which relies on the prior procedures.</p>
<p>Currently, the SymPy’s default method of zero testing <code class="docutils literal notranslate"><span class="pre">_iszero</span></code> is only
guaranteed to be accurate in some limited domain of numerics and symbols,
and any complicated expressions beyond its decidability are treated as <code class="docutils literal notranslate"><span class="pre">None</span></code>,
which behaves similarly to logical <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>The list of methods using zero testing procedures are as follows:</p>
<p><code class="docutils literal notranslate"><span class="pre">echelon_form</span></code> , <code class="docutils literal notranslate"><span class="pre">is_echelon</span></code> , <code class="docutils literal notranslate"><span class="pre">rank</span></code> , <code class="docutils literal notranslate"><span class="pre">rref</span></code> , <code class="docutils literal notranslate"><span class="pre">nullspace</span></code> ,
<code class="docutils literal notranslate"><span class="pre">eigenvects</span></code> , <code class="docutils literal notranslate"><span class="pre">inverse_ADJ</span></code> , <code class="docutils literal notranslate"><span class="pre">inverse_GE</span></code> , <code class="docutils literal notranslate"><span class="pre">inverse_LU</span></code> ,
<code class="docutils literal notranslate"><span class="pre">LUdecomposition</span></code> , <code class="docutils literal notranslate"><span class="pre">LUdecomposition_Simple</span></code> , <code class="docutils literal notranslate"><span class="pre">LUsolve</span></code></p>
<p>They have property <code class="docutils literal notranslate"><span class="pre">iszerofunc</span></code> opened up for user to specify zero testing
method, which can accept any function with single input and boolean output,
while being defaulted with <code class="docutils literal notranslate"><span class="pre">_iszero</span></code>.</p>
<p>Here is an example of solving an issue caused by undertested zero. While the
output for this particular matrix has since been improved, the technique
below is still of interest.
<a class="footnote-reference brackets" href="#zerotestexampleidea-fn" id="id2">1</a> <a class="footnote-reference brackets" href="#zerotestexamplediscovery-fn" id="id3">2</a>
<a class="footnote-reference brackets" href="#zerotestexampleimproved-fn" id="id4">3</a></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s2">&quot;q&quot;</span><span class="p">,</span> <span class="n">positive</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([</span>
<span class="gp">... </span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">cosh</span><span class="p">(</span><span class="n">q</span><span class="o">/</span><span class="mi">3</span><span class="p">),</span>      <span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">q</span><span class="p">),</span>            <span class="mi">1</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span>      <span class="n">exp</span><span class="p">(</span><span class="n">q</span><span class="p">),</span> <span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">cosh</span><span class="p">(</span><span class="n">q</span><span class="o">/</span><span class="mi">3</span><span class="p">),</span>            <span class="mi">1</span><span class="p">],</span>
<span class="gp">... </span><span class="p">[</span>           <span class="mi">1</span><span class="p">,</span>            <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="o">*</span><span class="n">cosh</span><span class="p">(</span><span class="n">q</span><span class="o">/</span><span class="mi">3</span><span class="p">)]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">nullspace</span><span class="p">()</span> 
<span class="go">[]</span>
</pre></div>
</div>
<p>You can trace down which expression is being underevaluated,
by injecting a custom zero test with warnings enabled.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">warnings</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_iszero</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">result</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">is_zero</span>
<span class="gp">... </span>    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">result</span> <span class="o">=</span> <span class="kc">None</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="c1"># Warnings if evaluated into None</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Zero testing of </span><span class="si">{}</span><span class="s2"> evaluated into None&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">result</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">nullspace</span><span class="p">(</span><span class="n">iszerofunc</span><span class="o">=</span><span class="n">my_iszero</span><span class="p">)</span> 
<span class="go">__main__:9: UserWarning: Zero testing of 4*cosh(q/3)**2 - 1 evaluated into None</span>
<span class="go">__main__:9: UserWarning: Zero testing of (-exp(q) - 2*cosh(q/3))*(-2*cosh(q/3) - exp(-q)) - (4*cosh(q/3)**2 - 1)**2 evaluated into None</span>
<span class="go">__main__:9: UserWarning: Zero testing of 2*exp(q)*cosh(q/3) - 16*cosh(q/3)**4 + 12*cosh(q/3)**2 + 2*exp(-q)*cosh(q/3) evaluated into None</span>
<span class="go">__main__:9: UserWarning: Zero testing of -(4*cosh(q/3)**2 - 1)*exp(-q) - 2*cosh(q/3) - exp(-q) evaluated into None</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>In this case,
<code class="docutils literal notranslate"><span class="pre">(-exp(q)</span> <span class="pre">-</span> <span class="pre">2*cosh(q/3))*(-2*cosh(q/3)</span> <span class="pre">-</span> <span class="pre">exp(-q))</span> <span class="pre">-</span> <span class="pre">(4*cosh(q/3)**2</span> <span class="pre">-</span> <span class="pre">1)**2</span></code>
should yield zero, but the zero testing had failed to catch.
possibly meaning that a stronger zero test should be introduced.
For this specific example, rewriting to exponentials and applying simplify would
make zero test stronger for hyperbolics,
while being harmless to other polynomials or transcendental functions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_iszero</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">result</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">rewrite</span><span class="p">(</span><span class="n">exp</span><span class="p">)</span><span class="o">.</span><span class="n">simplify</span><span class="p">()</span><span class="o">.</span><span class="n">is_zero</span>
<span class="gp">... </span>    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">result</span> <span class="o">=</span> <span class="kc">None</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="c1"># Warnings if evaluated into None</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Zero testing of </span><span class="si">{}</span><span class="s2"> evaluated into None&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">result</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">nullspace</span><span class="p">(</span><span class="n">iszerofunc</span><span class="o">=</span><span class="n">my_iszero</span><span class="p">)</span> 
<span class="go">__main__:9: UserWarning: Zero testing of -2*cosh(q/3) - exp(-q) evaluated into None</span>
<span class="go">⎡⎡  ⎛   q         ⎛q⎞⎞  -q         2⎛q⎞    ⎤⎤</span>
<span class="go">⎢⎢- ⎜- ℯ  - 2⋅cosh⎜─⎟⎟⋅ℯ   + 4⋅cosh ⎜─⎟ - 1⎥⎥</span>
<span class="go">⎢⎢  ⎝             ⎝3⎠⎠              ⎝3⎠    ⎥⎥</span>
<span class="go">⎢⎢─────────────────────────────────────────⎥⎥</span>
<span class="go">⎢⎢          ⎛      2⎛q⎞    ⎞     ⎛q⎞       ⎥⎥</span>
<span class="go">⎢⎢        2⋅⎜4⋅cosh ⎜─⎟ - 1⎟⋅cosh⎜─⎟       ⎥⎥</span>
<span class="go">⎢⎢          ⎝       ⎝3⎠    ⎠     ⎝3⎠       ⎥⎥</span>
<span class="go">⎢⎢                                         ⎥⎥</span>
<span class="go">⎢⎢           ⎛   q         ⎛q⎞⎞            ⎥⎥</span>
<span class="go">⎢⎢          -⎜- ℯ  - 2⋅cosh⎜─⎟⎟            ⎥⎥</span>
<span class="go">⎢⎢           ⎝             ⎝3⎠⎠            ⎥⎥</span>
<span class="go">⎢⎢          ────────────────────           ⎥⎥</span>
<span class="go">⎢⎢                   2⎛q⎞                  ⎥⎥</span>
<span class="go">⎢⎢             4⋅cosh ⎜─⎟ - 1              ⎥⎥</span>
<span class="go">⎢⎢                    ⎝3⎠                  ⎥⎥</span>
<span class="go">⎢⎢                                         ⎥⎥</span>
<span class="go">⎣⎣                    1                    ⎦⎦</span>
</pre></div>
</div>
<p>You can clearly see <code class="docutils literal notranslate"><span class="pre">nullspace</span></code> returning proper result, after injecting an
alternative zero test.</p>
<p>Note that this approach is only valid for some limited cases of matrices
containing only numerics, hyperbolics, and exponentials.
For other matrices, you should use different method opted for their domains.</p>
<p>Possible suggestions would be either taking advantage of rewriting and
simplifying, with tradeoff of speed <a class="footnote-reference brackets" href="#zerotestsimplifysolution-fn" id="id5">4</a> ,
or using random numeric testing, with tradeoff of accuracy
<a class="footnote-reference brackets" href="#zerotestnumerictestsolution-fn" id="id6">5</a> .</p>
<p>If you wonder why there is no generic algorithm for zero testing that can work
with any symbolic entities,
it’s because of the constant problem stating that zero testing is undecidable
<a class="footnote-reference brackets" href="#constantproblemwikilink-fn" id="id7">6</a> ,
and not only the SymPy, but also other computer algebra systems
<a class="footnote-reference brackets" href="#mathematicazero-fn" id="id8">7</a> <a class="footnote-reference brackets" href="#matlabzero-fn" id="id9">8</a>
would face the same fundamental issue.</p>
<p>However, discovery of any zero test failings can provide some good examples to
improve SymPy,
so if you have encountered one, you can report the issue to
SymPy issue tracker <a class="footnote-reference brackets" href="#sympyissues-fn" id="id10">9</a> to get detailed help from the community.</p>
<p class="rubric">Footnotes</p>
<dl class="footnote brackets">
<dt class="label" id="zerotestexampleidea-fn"><span class="brackets"><a class="fn-backref" href="#id2">1</a></span></dt>
<dd><p>Inspired by <a class="reference external" href="https://gitter.im/sympy/sympy?at=5b7c3e8ee5b40332abdb206c">https://gitter.im/sympy/sympy?at=5b7c3e8ee5b40332abdb206c</a></p>
</dd>
<dt class="label" id="zerotestexamplediscovery-fn"><span class="brackets"><a class="fn-backref" href="#id3">2</a></span></dt>
<dd><p>Discovered from <a class="reference external" href="https://github.com/sympy/sympy/issues/15141">https://github.com/sympy/sympy/issues/15141</a></p>
</dd>
<dt class="label" id="zerotestexampleimproved-fn"><span class="brackets"><a class="fn-backref" href="#id4">3</a></span></dt>
<dd><p>Improved by <a class="reference external" href="https://github.com/sympy/sympy/pull/19548">https://github.com/sympy/sympy/pull/19548</a></p>
</dd>
<dt class="label" id="zerotestsimplifysolution-fn"><span class="brackets"><a class="fn-backref" href="#id5">4</a></span></dt>
<dd><p>Suggested from <a class="reference external" href="https://github.com/sympy/sympy/issues/10120">https://github.com/sympy/sympy/issues/10120</a></p>
</dd>
<dt class="label" id="zerotestnumerictestsolution-fn"><span class="brackets"><a class="fn-backref" href="#id6">5</a></span></dt>
<dd><p>Suggested from <a class="reference external" href="https://github.com/sympy/sympy/issues/10279">https://github.com/sympy/sympy/issues/10279</a></p>
</dd>
<dt class="label" id="constantproblemwikilink-fn"><span class="brackets"><a class="fn-backref" href="#id7">6</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Constant_problem">https://en.wikipedia.org/wiki/Constant_problem</a></p>
</dd>
<dt class="label" id="mathematicazero-fn"><span class="brackets"><a class="fn-backref" href="#id8">7</a></span></dt>
<dd><p>How mathematica tests zero <a class="reference external" href="https://reference.wolfram.com/language/ref/PossibleZeroQ.html">https://reference.wolfram.com/language/ref/PossibleZeroQ.html</a></p>
</dd>
<dt class="label" id="matlabzero-fn"><span class="brackets"><a class="fn-backref" href="#id9">8</a></span></dt>
<dd><p>How matlab tests zero <a class="reference external" href="https://www.mathworks.com/help/symbolic/mupad_ref/iszero.html">https://www.mathworks.com/help/symbolic/mupad_ref/iszero.html</a></p>
</dd>
<dt class="label" id="sympyissues-fn"><span class="brackets"><a class="fn-backref" href="#id10">9</a></span></dt>
<dd><p><a class="reference external" href="https://github.com/sympy/sympy/issues">https://github.com/sympy/sympy/issues</a></p>
</dd>
</dl>
</section>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Matrices</a><ul>
<li><a class="reference internal" href="#basic-operations">Basic Operations</a><ul>
<li><a class="reference internal" href="#shape">Shape</a></li>
<li><a class="reference internal" href="#accessing-rows-and-columns">Accessing Rows and Columns</a></li>
<li><a class="reference internal" href="#deleting-and-inserting-rows-and-columns">Deleting and Inserting Rows and Columns</a></li>
</ul>
</li>
<li><a class="reference internal" href="#basic-methods">Basic Methods</a></li>
<li><a class="reference internal" href="#matrix-constructors">Matrix Constructors</a></li>
<li><a class="reference internal" href="#advanced-methods">Advanced Methods</a><ul>
<li><a class="reference internal" href="#determinant">Determinant</a></li>
<li><a class="reference internal" href="#rref">RREF</a></li>
<li><a class="reference internal" href="#nullspace">Nullspace</a></li>
<li><a class="reference internal" href="#columnspace">Columnspace</a></li>
<li><a class="reference internal" href="#eigenvalues-eigenvectors-and-diagonalization">Eigenvalues, Eigenvectors, and Diagonalization</a></li>
</ul>
</li>
<li><a class="reference internal" href="#possible-issues">Possible Issues</a><ul>
<li><a class="reference internal" href="#zero-testing">Zero Testing</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="solvers.html"
                        title="previous chapter">Solvers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="manipulation.html"
                        title="next chapter">Advanced Expression Manipulation</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/tutorial/matrices.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="manipulation.html" title="Advanced Expression Manipulation"
             >next</a> |</li>
        <li class="right" >
          <a href="solvers.html" title="Solvers"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Matrices</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/tutorial/matrices.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:58 GMT -->
</html>